﻿
using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Data
{
    using SharpSoft.Data.Expressions;
    using SharpSoft.Data.GSQL;
    public class MySqlTextGenerator : SQLTextGenerator
    {
        public override string LastAutoId => "LAST_INSERT_ID()";

        public override string GetNow => "NOW()";

        protected override string ProcessExpression(BinaryExpression bin)
        {
            return base.ProcessExpression(bin);
        }

        protected override string ProcessDataType(DataType dataType)
        {
            switch (dataType)
            {
                case DataType.Binary:
                    return "LONGBLOB";
                case DataType.Long:
                    return "BIGINT";
                case DataType.DateTime:
                    return "DATETIME";
                case DataType.Date:
                    return "DATE";
                case DataType.Time:
                    return "TIME";
                case DataType.Guid:
                    return "BINARY(16)";
                case DataType.Text:
                    return "LONGTEXT";
                case DataType.Boolean:
                    return "BIT";
                case DataType.Char:
                case DataType.NChar:
                case DataType.VarChar:
                case DataType.NVarChar:
                case DataType.Int:
                case DataType.Decimal:


                default:
                    return base.ProcessDataType(dataType);
            }
        }

        protected override NativeSQLCommandScript ProcessStatement(CreateTableStatement statement)
        {
            var basesql = base.ProcessStatement(statement).CommandText;
            List<string> list = new List<string>();
            if (!string.IsNullOrEmpty(statement.Engine))
            {
                list.Add($"ENGINE = {statement.Engine}");
            }
            if (!string.IsNullOrEmpty(statement.DefaultCharset))
            {
                list.Add($"DEFAULT CHARSET = {statement.DefaultCharset}");
            }
            if (!string.IsNullOrEmpty(statement.Comment))
            {
                list.Add($"COMMENT = {new SqlValue(statement.Comment)}");
            }
            return basesql + String.Join(' ', list.ToArray());
        }
    }
}
